package benchmark;

import java.util.*;
import java.util.concurrent.Callable;

public class FSArray extends AbstractMTTask {

	private class FSArrayCallable implements Callable<String> {
		
		long[] array;
		int position;
		
		public FSArrayCallable(long[] array, int position) {
			this.array = array;
			this.position = position;
		}
		
		@Override
		public String call() throws Exception {
			long counter = iterTime;
			while (counter-- > 0) {
				array[position] += counter + position;
			}
			// return sum of the entire array to avoid optimize
			long sum = 0;
			for (int i = 0; i < array.length; i++) {
				sum += array[i];
			}
			return Long.toString(sum);
		}
		
	}
	
	private volatile long[] array;

	long iterTime;

	/* 
	 * @param param
	 * 0. class name
	 * 1. thread number
	 * 2. stride
	 * 3. iteration time
	 */
	@Override
	protected List<Callable<String>> initCallables(List<String> param) {
		int stride = Integer.parseInt(param.get(2));
		iterTime = Long.parseLong(param.get(3));
		
		array = new long[stride * threadNum];
		
		// init array
		for (int i = 0; i < array.length; i++) {
			array[i] = i;
		}
		
		List<Callable<String>> l = new ArrayList<Callable<String>>();
		for (int i = 0; i < this.threadNum; i++) {
			l.add(new FSArrayCallable(array, stride * i));
		}
		return l;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
